#pragma once
#include <assert.h>
namespace  bit
{
	template<class T>
	class vector
	{
	public:
		typedef T* itrator;
		typedef const T* const iterator;
		iterator begin()
		{
			return _finish;
		}
		const itrator begin()const
		{
			return _finish;
		}
		iterator end()
		{
			return _end_of_storage;
		}
		const itrator end()const
		{
			return _end_of_storage;
		}
		template<class inputiterator>
		vector(inputiterator first, inputiterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++:
			}
		}
		vector(size_t n, const T& val = T())
		{
			reserve(n);
			for (int i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		vector(int n, const T& val = T())
		{
			reserve(n);
			for (int i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		vector(initializer_list<T> il)
		{
			reserve(il.size());
			for (auto e : il)
			{
				push_back(e);
			}

		}
		vector(const vector<T>& v)
		{
			reserve(v.size());
			for (auto e : v)
			{
				push_back(e);
			}
		}
		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finsh, v._finsh);
			std::swap(_end_of_storage, v._end_of_storage);
		}
		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}
		~vector()
		{
			if (_start)
			{
				delete[] _start;
				_start = _finsh = _end_of_storage = nullptr;
			}
		}
		size_t  size()const
		{
			return _finsh - _start;
		}
		size_t capacity()const
		{
			return _end_of_storage - _start;
		}
		void reserve()
		{
			if (n > capacity)
			{
				size_t  oldsize = size();
				T* tmp = new T[n];
				if (_start)
				{
					for (int i = 0; i < oldsize; i++)
					{
						tmp[i] = _start[i];
					}
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + oldsize;
				_end_of_storage = _start + n;
			}
		}
		const T* operator[](size_ i)const
		{
			assert(i < size())
				return _start[i];
		}
		void push_back(const  T& x)
		{
			if (_finish == _end_of_storage)
			{
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
			}
			*_finish = x;
			_finish++;
		}
		void pop_back()
		{
			--_finish;
		}
		void erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish)
				iterator  it = pos + 1;
			while (it != _finish)
			{
				*(it - 1) = *it;
				it++;
			}
			_finish--;
		}
		iterator insert(iterator pos, const T& x)
		{
			assert(pos >= _start);
			assert(pos <= _finish);
			if (_finish == _end_of_storage)
			{
				size_t len = pos - _start;
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
				pos = _start + len;
			}
			size_t end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				end--;
			}
			*pos = x;
			_finish++;
			return pos;
		}
	private:
		iterator  _start;
		iterator _finish;
		iterator _end_of_storage;
	};
}
